﻿unit smOrderFrame;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, SystemBaseFrame, Data.DB, Data.Win.ADODB,
  Vcl.ExtCtrls, RzPanel, RzSplit, cxGraphics, cxControls, cxLookAndFeels,
  cxLookAndFeelPainters, dxSkinsCore, dxSkinBlack, dxSkinBlue, dxSkinBlueprint,
  dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide,
  dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray, dxSkinOffice2013LightGray,
  dxSkinOffice2013White, dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic,
  dxSkinSharp, dxSkinSharpPlus, dxSkinSilver, dxSkinSpringTime, dxSkinStardust,
  dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinsDefaultPainters,
  dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue, cxStyles,
  dxSkinscxPCPainter, cxCustomData, cxFilter, cxData, cxDataStorage, cxEdit,
  cxNavigator, cxDBData, cxGridLevel, cxClasses, cxGridCustomView,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGrid,
  Vcl.StdCtrls, DBGridEh, Vcl.Mask, DBCtrlsEh, DBLookupEh, cxContainer, RzLabel,
  cxTextEdit, cxMaskEdit, cxDropDownEdit, cxLookupEdit, cxDBLookupEdit,
  cxDBLookupComboBox, RzButton,Datasnap.DBClient,qjson,

  SK0001Comm,PYIndexUnit,System.StrUtils,

  CmCustomerFileModel,CmLinkmanModel,PmItemOtherModel,PmMsunitModel,PmPackageTypeModel,PmRanksModel,
  SmChargeItemModel,SmInvoiceTypeModel,SmPaywayModel,SmShipTypeModel,
  PmItemDataModel,SmOrderMstModel,SmOrderModel,SmOrderDtlModel,SysDepartmentModel,
  CmPackageAccountModel,SmOrderChargeModel,

  SmOrderMstService,CmCustomerFileService,CmLinkmanService,PmItemOtherService,
  PmMsunitService,PmPackageTypeService,PmRanksService,SmChargeItemService,
  SmInvoiceTypeService,SmPaywayService,SmShipTypeService,PmItemDataService,
  SmOrderDtlService,SmOrderChargeService,

  DBGridEhGrouping,ToolCtrlsEh, DBGridEhToolCtrls, DynVarsEh, GridsEh, DBAxisGridsEh,
  cxBlobEdit, cxCurrencyEdit, cxSpinEdit, cxCalc, cxGridBandedTableView,
  cxGridDBBandedTableView, System.Actions, Vcl.ActnList, Vcl.ImgList;

type
  TFrame_smOrder = class(TFrame_SytemBase)
    RzPanel_Mst: TRzPanel;
    LabelEdit_OrderId: TLabeledEdit;
    cxLookupComboBox_Customer: TcxLookupComboBox;
    RzLabel_CustomerId: TRzLabel;
    cxLookupComboBox_InvoiceType: TcxLookupComboBox;
    RzLabel_InvoiceType: TRzLabel;
    RzLabel_Payway: TRzLabel;
    cxLookupComboBox_Payway: TcxLookupComboBox;
    RzLabel_ShipType: TRzLabel;
    cxLookupComboBox_ShipType: TcxLookupComboBox;
    cxLookupComboBox_EmpId: TcxLookupComboBox;
    RzLabel_EmpId: TRzLabel;
    Cds_Customer: TClientDataSet;
    Ds_Customer: TDataSource;
    Ds_ItemOther: TDataSource;
    Cds_ItemOther: TClientDataSet;
    Ds_Msunit: TDataSource;
    Cds_Msunit: TClientDataSet;
    Ds_PackageType: TDataSource;
    Cds_PackageType: TClientDataSet;
    Ds_Ranks: TDataSource;
    Cds_Ranks: TClientDataSet;
    Ds_ChargeItem: TDataSource;
    Cds_ChargeItem: TClientDataSet;
    Ds_InvoiceType: TDataSource;
    Cds_InvoiceType: TClientDataSet;
    Ds_Payway: TDataSource;
    Cds_Payway: TClientDataSet;
    Ds_ShipType: TDataSource;
    Cds_ShipType: TClientDataSet;
    Ds_ItemData: TDataSource;
    Cds_ItemData: TClientDataSet;
    Ds_OrderDtl: TDataSource;
    Cds_OrderDtl: TClientDataSet;
    cxGrid_OrderDtlLevel1: TcxGridLevel;
    cxGrid_OrderDtl: TcxGrid;
    cxGrid_OrderDtlDBBandedTableView1: TcxGridDBBandedTableView;
    RzPanel_OrderDtl: TRzPanel;
    cxLookupComboBox_Linkman: TcxLookupComboBox;
    RzLabel_Linkman: TRzLabel;
    Ds_Linkman: TDataSource;
    Cds_Linkman: TClientDataSet;
    LabelEdit_Remark: TLabeledEdit;
    RzLabel_Dept: TRzLabel;
    Ds_Dept: TDataSource;
    Cds_Dept: TClientDataSet;
    cxLookupComboBox_Dept: TcxLookupComboBox;
    ImageList_ToolBar: TImageList;
    ActionList_Main: TActionList;
    Action_SaveAndView: TAction;
    Action_SaveAndClose: TAction;
    Action_Close: TAction;
    Action_AddOrderDtlRow: TAction;
    Action_DeleteOrderDtlRow: TAction;
    RzToolBar_Main: TRzToolbar;
    RzToolButton_SaveAndBack: TRzToolButton;
    RzToolButton_SaveAndView: TRzToolButton;
    RzToolButton_Close: TRzToolButton;
    RzToolBar_Detail: TRzToolbar;
    RzToolButton_AddOrderDtlRow: TRzToolButton;
    RzToolButton_DeleteOrderDtlRow: TRzToolButton;
    cxGrid_ChargeItem: TcxGrid;
    cxGridTableView_ChargeItem: TcxGridDBBandedTableView;
    cxGridLevel_ChargeItem: TcxGridLevel;
    RzToolBar_ChargeItem: TRzToolbar;
    RzToolButton_AddChargeItemRow: TRzToolButton;
    RzToolButton_DeleteChargeItemRow: TRzToolButton;
    RzGroupBox_ChargeItem: TRzGroupBox;
    Action_AddChargeItemRow: TAction;
    Action_DeleteChargeItemRow: TAction;
    Ds_OrderCharge: TDataSource;
    Cds_OrderCharge: TClientDataSet;
    RzSplitter_Main: TRzSplitter;
    RzSplitter_H: TRzSplitter;
    procedure cxLookupComboBox_CustomerIdPropertiesChange(Sender: TObject);
    procedure Action_SaveAndViewExecute(Sender: TObject);
    procedure Action_SaveAndCloseExecute(Sender: TObject);
    procedure Action_CloseExecute(Sender: TObject);
    procedure Action_AddOrderDtlRowExecute(Sender: TObject);
    procedure Action_DeleteOrderDtlRowExecute(Sender: TObject);
    procedure Action_AddChargeItemRowExecute(Sender: TObject);
    procedure Action_DeleteChargeItemRowExecute(Sender: TObject);


  private
    { Private declarations }

    fOrderListManageKey:string;

    fLoginOrgCustomerFileList:TCmCustomerFileList;
    fLoginOrgItemOtherList:TPmItemOtherList;
    fLoginOrgItemDataList:TPmItemDataList;
    fLoginOrgMsunitList:TPmMsunitList;
    fLoginOrgPackageTypeList:TPmPackageTypeList;
    fLoginOrgRanksList:TPmRanksList;
    fLoginOrgChargeItemList:TSmChargeItemList;
    fLoginOrgInvoiceTypeList:TSmInvoiceTypeList;
    fLoginOrgPaywayList:TSmPaywayList;
    fLoginOrgShipTypeList:TSmShipTypeList;
    fSelectCustomerLinkmanList:TCmLinkmanList;

    fOrderService:TSmOrderService;
    fOrderDtlService:TSmOrderDtlService;
    fCustomerFileService:TCmCustomerFileService;
    fItemOtherService:TPmItemOtherService;
    fItemDataService:TPmItemDataService;
    fMsunitService:TPmMsunitService;
    fPackageTypeService:TPmPackageTypeService;
    fRanksService:TPmRanksService;
    fChargeItemService:TSmChargeItemService;
    fInvoiceTypeService:TSmInvoiceTypeService;
    fPaywayService:TSmPaywayService;
    fShipTypeService:TSmShipTypeService;
    fLinkmanService:TCmLinkmanService;
    fOrderChargeService:TSmOrderChargeService;

    fCurrOrder:TSmOrder;

    function InitOrderMast(out oError:string):Boolean;
    function InitOrderDetail(out oError:string):Boolean;
    function InitOrderCharge(out oError:string):Boolean;

    procedure RefreshComponent(const iItem:TSmOrder; const iRunMode:eRunMode);
    procedure RefreshButton(const iItem:TSmOrder; const iRunMode:eRunMode);
    procedure RefreshFrame(const iItem:TSmOrder; const iRunMode:eRunMode);
    procedure Obj2Frame(const iItem:TSmOrder; const iRunMode:eRunMode);
    procedure Frame2Obj(const iItem:TSmOrder; const iRunMode:eRunMode);

    procedure OnOrderDtlCdsValidate(Sender:TField);
    procedure OnOrderDtlLookComboboxCloseUp(Sender:TObject);
    procedure OnFooterSummary(ASender: TcxDataSummaryItems; Arguments: TcxSummaryEventArguments; var OutArguments: TcxSummaryEventOutArguments);
  public
    { Public declarations }
    constructor Create(const iOwner:TWinControl; const iMainWindowHandle:HWND; const iSKSOFTConn,iSK0001Conn:TADOConnection); overload;
    destructor Destroy();override;

    procedure InitFrame(const iManageKey:string; const iRunMode:eRunMode; const iBillNo:string; const iParamStr:string);override;
  end;

  function GetInstance(const iApplicationHandle:HWND; const iParentWinControlPtr:Integer; const iMainWindowHandle:HWND; const iSKSOFT,iSK0001:TAdoConnection):TFrame_SytemBase;stdcall;

var
  Frame_smOrder: TFrame_smOrder;

implementation

{$R *.dfm}

function GetInstance(const iApplicationHandle:HWND; const iParentWinControlPtr:Integer; const iMainWindowHandle:HWND; const iSKSOFT,iSK0001:TAdoConnection):TFrame_SytemBase;stdcall;
begin
  Application.Handle:=iApplicationHandle;
  Result:=TFrame_smOrder.Create(TWinControl(Pointer(iParentWinControlPtr)),iMainWindowHandle,iSKSOFT,iSK0001);
end;

procedure TFrame_smOrder.Action_CloseExecute(Sender: TObject);
begin
  inherited;
  CloseTabPage(fManageKey,fOrderListManageKey);
end;

procedure TFrame_smOrder.Action_AddOrderDtlRowExecute(Sender: TObject);
begin
  inherited;
  Cds_OrderDtl.Append;
end;

procedure TFrame_smOrder.Action_DeleteOrderDtlRowExecute(Sender: TObject);
begin
  inherited;
  Cds_OrderDtl.Delete;
end;

procedure TFrame_smOrder.Action_AddChargeItemRowExecute(Sender: TObject);
begin
  inherited;
  Cds_OrderCharge.Append;
end;

procedure TFrame_smOrder.Action_DeleteChargeItemRowExecute(Sender: TObject);
begin
  inherited;
  Cds_OrderCharge.Delete;
end;

procedure TFrame_smOrder.Action_SaveAndCloseExecute(Sender: TObject);
var
  Error:string;
begin
  inherited;
  case fRunMode of
  rmNew:
    begin
      Frame2Obj(fCurrOrder,fRunMode);
      if fOrderService.Add(fCurrOrder,Error) then
      begin
        ShowMessage('保存成功');
      end else begin
        ShowMessage(Format(string('保存失败,错误内容:%s'),[Error]));
      end;
      CloseTabPage(fManageKey,fOrderListManageKey);
    end;
  rmEdit:
    begin
      Frame2Obj(fCurrOrder,fRunMode);
      if fOrderService.Edit(fCurrOrder,Error) then
      begin
        ShowMessage('保存成功');
      end else begin
        ShowMessage(Format(string('保存失败,错误内容:%s'),[Error]));
      end;
      CloseTabPage(fManageKey,fOrderListManageKey);
    end;
  end;
end;

procedure TFrame_smOrder.Action_SaveAndViewExecute(Sender: TObject);
var
  Error:string;
begin
  inherited;
  case fRunMode of
  rmNew:
    begin
        Frame2Obj(fCurrOrder,fRunMode);
        if fOrderService.Add(fCurrOrder,Error) then
        begin
          ShowMessage('保存成功');
        end else begin
          ShowMessage(Format(string('保存失败,错误内容:%s'),[Error]));
        end;
        fRunMode:=rmView;
        RefreshFrame(fCurrOrder,fRunMode);
    end;
  rmEdit:
    begin
      Frame2Obj(fCurrOrder,fRunMode);
      if fOrderService.Edit(fCurrOrder,Error) then
      begin
        ShowMessage('保存成功');
      end else begin
        ShowMessage(Format(string('保存失败,错误内容:%s'),[Error]));
      end;
      fRunMode:=rmView;
      RefreshFrame(fCurrOrder,fRunMode);
    end;
  end;
end;

constructor TFrame_smOrder.Create(const iOwner:TWinControl; const iMainWindowHandle:HWND; const iSKSOFTConn,iSK0001Conn:TADOConnection);
begin
  inherited Create(iOwner,iMainWindowHandle,iSKSOFTConn,iSK0001Conn);
  Self.Parent:=iOwner;
  Self.Align:=alClient;

  fOrderListManageKey:=EmptyStr;

  fLoginOrgCustomerFileList:=TCmCustomerFileList.Create;
  fLoginOrgItemOtherList:=TPmItemOtherList.Create;
  fLoginOrgItemDataList:=TPmItemDataList.Create;
  fLoginOrgMsunitList:=TPmMsunitList.Create;
  fLoginOrgPackageTypeList:=TPmPackageTypeList.Create;
  fLoginOrgRanksList:=TPmRanksList.Create;
  fLoginOrgChargeItemList:=TSmChargeItemList.Create;
  fLoginOrgInvoiceTypeList:=TSmInvoiceTypeList.Create;
  fLoginOrgPaywayList:=TSmPaywayList.Create;
  fLoginOrgShipTypeList:=TSmShipTypeList.Create;
  fSelectCustomerLinkmanList:=TCmLinkmanList.Create;

  fCurrOrder:=TSmOrder.Create;

  fOrderService:=TSmOrderService.Create(fSK0001Conn,fSK0001Conn,EmptyStr);
  fOrderDtlService:=TSmOrderDtlService.Create(fSK0001Conn,fSK0001Conn,EmptyStr);
  fCustomerFileService:=TCmCustomerFileService.Create(fSK0001Conn,fSK0001Conn,EmptyStr);
  fItemOtherService:=TPmItemOtherService.Create(fSK0001Conn,fSK0001Conn,EmptyStr);
  fItemDataService:=TPmItemDataService.Create(fSK0001Conn,fSK0001Conn,EmptyStr);
  fMsunitService:=TPmMsunitService.Create(fSK0001Conn,fSK0001Conn,EmptyStr);
  fPackageTypeService:=TPmPackageTypeService.Create(fSK0001Conn,fSK0001Conn,EmptyStr);
  fRanksService:=TPmRanksService.Create(fSK0001Conn,fSK0001Conn,EmptyStr);
  fChargeItemService:=TSmChargeItemService.Create(fSK0001Conn,fSK0001Conn,EmptyStr);
  fInvoiceTypeService:=TSmInvoiceTypeService.Create(fSK0001Conn,fSK0001Conn,EmptyStr);
  fPaywayService:=TSmPaywayService.Create(fSK0001Conn,fSK0001Conn,EmptyStr);
  fShipTypeService:=TSmShipTypeService.Create(fSK0001Conn,fSK0001Conn,EmptyStr);
  fLinkmanService:=TCmLinkmanService.Create(fSK0001Conn,fSK0001Conn,EmptyStr);
  fOrderChargeService:=TSmOrderChargeService.Create(fSK0001Conn,fSK0001Conn,EmptyStr);

  fRunMode:=rmView;
end;

procedure TFrame_smOrder.cxLookupComboBox_CustomerIdPropertiesChange(
  Sender: TObject);
var
  Condition,OrderBy,Error:string;
  LinkmanItem:TCmLinkman;
begin
  inherited;
  cxLookupComboBox_Linkman.EditValue:=EmptyStr;
  Condition:=Format(string('%s=''%s'' and %s=''%s'' '),
                  [TCmLinkmanModel.cEnterpriseIdKey,cxLookupComboBox_Customer.EditValue,
                  TCmLinkmanModel.cOrgIdKey,fLoginOrg.OrgId]);
  OrderBy:=Format(string('%s ASC'),[TCmLinkmanModel.cLinkmanIdKey]);
  if fLinkmanService.LoadByCondition(fSelectCustomerLinkmanList,Condition,Error) and
    fLinkmanService.GetCDSModel(Cds_Linkman,Error) and
    fSelectCustomerLinkmanList.SaveToCDS(Cds_Linkman,Error) then
  begin
    for LinkmanItem in fSelectCustomerLinkmanList.Values do
    begin
      if LinkmanItem.LinkmanIsMain then
      begin
        cxLookupComboBox_Linkman.EditValue:=LinkmanItem.LinkmanId;
        Break;
      end;
    end;
  end;
end;

destructor TFrame_smOrder.Destroy();
begin
  fLoginOrgCustomerFileList.Free;
  fLoginOrgItemOtherList.Free;
  fLoginOrgItemDataList.Free;
  fLoginOrgMsunitList.Free;
  fLoginOrgPackageTypeList.Free;
  fLoginOrgRanksList.Free;
  fLoginOrgChargeItemList.Free;
  fLoginOrgInvoiceTypeList.Free;
  fLoginOrgPaywayList.Free;
  fLoginOrgShipTypeList.Free;
  fSelectCustomerLinkmanList.Free;

  fCurrOrder.Free;

  fOrderService.Free;
  fOrderDtlService.Free;
  fCustomerFileService.Free;
  fItemOtherService.Free;
  fItemDataService.Free;
  fMsunitService.Free;
  fPackageTypeService.Free;
  fRanksService.Free;
  fChargeItemService.Free;
  fInvoiceTypeService.Free;
  fPaywayService.Free;
  fShipTypeService.Free;
  fLinkmanService.Free;
  fOrderChargeService.Free;

  inherited Destroy;
end;

function TFrame_smOrder.InitOrderMast(out oError:string):Boolean;
begin
  with cxLookupComboBox_Dept.Properties.ListColumns.Add  do
  begin
    Caption:='部门名称';
    FieldName:=TSysDepartmentModel.cDeptFullNameKey;
    MinWidth:=100;
    Width:=100;
  end;
  cxLookupComboBox_Dept.Properties.ListSource:=Ds_Dept;
  cxLookupComboBox_Dept.Properties.DropDownListStyle:=TcxEditDropDownListStyle.lsFixedList;
  cxLookupComboBox_Dept.Properties.DropDownAutoSize:=True;                                        //设置下拉列表为自适应宽度
//    cxLookupComboBox_Dept.Properties.ListFieldNames:=TSysDepartmentModel.cDeptFullNameKey;     //设置下拉列表需要显示的列
  cxLookupComboBox_Dept.Properties.KeyFieldNames:=TSysDepartmentModel.cDeptIdKey;            //设置返回的字段名，用EditValue取值
  cxLookupComboBox_Dept.Properties.ListFieldIndex:=0;

  cxLookupComboBox_Customer.Properties.ListSource:=Ds_Customer;
  with cxLookupComboBox_Customer.Properties.ListColumns.Add  do
  begin
    Caption:='客户编号';
    FieldName:=TCmCustomerFileModel.cCustomerIdKey;
    MinWidth:=100;
    Width:=100;
  end;
  with cxLookupComboBox_Customer.Properties.ListColumns.Add  do
  begin
    Caption:='客户名称';
    FieldName:=TCmCustomerFileModel.cCustomerFullNameKey;
    MinWidth:=100;
    Width:=300;
  end;
  with cxLookupComboBox_Customer.Properties.ListColumns.Add  do
  begin
    Caption:='客户名拼音';
    FieldName:='PinYin';
    MinWidth:=0;
    Width:=0;
  end;
  cxLookupComboBox_Customer.Properties.DropDownListStyle:=TcxEditDropDownListStyle.lsEditFixedList;
  cxLookupComboBox_Customer.Properties.DropDownAutoSize:=True;             //设置下拉列表为自适应宽度
//      cxLookupComboBox_Customer.Properties.ListFieldNames:=;                 //设置下拉列表需要显示的列
  cxLookupComboBox_Customer.Properties.KeyFieldNames:=TCmCustomerFileModel.cCustomerIdKey;            //设置返回的字段名，用EditValue取值
  cxLookupComboBox_Customer.Properties.ListFieldIndex:=1;                  //设置返回输入框中的值，用EditText取值

  with cxLookupComboBox_Linkman.Properties.ListColumns.Add  do
  begin
    Caption:='联系人姓名';
    FieldName:=TCmLinkmanModel.cLinkmanNameKey;
    MinWidth:=100;
    Width:=100;
  end;
  with cxLookupComboBox_Linkman.Properties.ListColumns.Add  do
  begin
    Caption:='联系电话';
    FieldName:=TCmLinkmanModel.cLinkmanPhoneKey;
    MinWidth:=100;
    Width:=100;
  end;
  with cxLookupComboBox_Linkman.Properties.ListColumns.Add  do
  begin
    Caption:='移动电话';
    FieldName:=TCmLinkmanModel.cLinkmanTelephoneKey;
    MinWidth:=100;
    Width:=100;
  end;
  with cxLookupComboBox_Linkman.Properties.ListColumns.Add  do
  begin
    Caption:='传真';
    FieldName:=TCmLinkmanModel.cLinkmanFaxKey;
    MinWidth:=100;
    Width:=100;
  end;
  with cxLookupComboBox_Linkman.Properties.ListColumns.Add  do
  begin
    Caption:='联系地址';
    FieldName:=TCmLinkmanModel.cLinkmanAddressKey;
    MinWidth:=100;
    Width:=100;
  end;
  cxLookupComboBox_Linkman.Properties.ListSource:=Ds_Linkman;
  cxLookupComboBox_Linkman.Properties.DropDownListStyle:=TcxEditDropDownListStyle.lsFixedList;
  cxLookupComboBox_Linkman.Properties.DropDownAutoSize:=True;                                        //设置下拉列表为自适应宽度
//    cxLookupComboBox_Linkman.Properties.ListFieldNames:=TSmShipTypeModel.cShipTypeNameKey;     //设置下拉列表需要显示的列
  cxLookupComboBox_Linkman.Properties.KeyFieldNames:=TCmLinkmanModel.cLinkmanIdKey;            //设置返回的字段名，用EditValue取值
  cxLookupComboBox_Linkman.Properties.ListFieldIndex:=0;

  with cxLookupComboBox_InvoiceType.Properties.ListColumns.Add  do
  begin
    Caption:='发票类型';
    FieldName:=TSmInvoiceTypeModel.cInvoiceTypeNameKey;
    MinWidth:=100;
    Width:=100;
  end;
  cxLookupComboBox_InvoiceType.Properties.ListSource:=Ds_InvoiceType;
  cxLookupComboBox_InvoiceType.Properties.DropDownListStyle:=TcxEditDropDownListStyle.lsFixedList;
  cxLookupComboBox_InvoiceType.Properties.DropDownAutoSize:=True;                                        //设置下拉列表为自适应宽度
//    cxLookupComboBox_InvoiceType.Properties.ListFieldNames:=TSmInvoiceTypeModel.cInvoiceTypeNameKey;     //设置下拉列表需要显示的列
  cxLookupComboBox_InvoiceType.Properties.KeyFieldNames:=TSmInvoiceTypeModel.cInvoiceTypeIdKey;            //设置返回的字段名，用EditValue取值
  cxLookupComboBox_InvoiceType.Properties.ListFieldIndex:=0;                                             //设置返回输入框中的值，用EditText取值

  with cxLookupComboBox_Payway.Properties.ListColumns.Add  do
  begin
    Caption:='支付方式';
    FieldName:=TSmPaywayModel.cPaywayNameKey;
    MinWidth:=100;
    Width:=100;
  end;
  cxLookupComboBox_Payway.Properties.ListSource:=Ds_Payway;
  cxLookupComboBox_Payway.Properties.DropDownListStyle:=TcxEditDropDownListStyle.lsFixedList;
  cxLookupComboBox_Payway.Properties.DropDownAutoSize:=True;                                        //设置下拉列表为自适应宽度
//    cxLookupComboBox_Payway.Properties.ListFieldNames:=TSmPaywayModel.cPaywayNameKey;     //设置下拉列表需要显示的列
  cxLookupComboBox_Payway.Properties.KeyFieldNames:=TSmPaywayModel.cPaywayIdKey;            //设置返回的字段名，用EditValue取值
  cxLookupComboBox_Payway.Properties.ListFieldIndex:=0;

  with cxLookupComboBox_ShipType.Properties.ListColumns.Add  do
  begin
    Caption:='运输方式';
    FieldName:=TSmShipTypeModel.cShipTypeNameKey;
    MinWidth:=100;
    Width:=100;
  end;
  cxLookupComboBox_ShipType.Properties.ListSource:=Ds_ShipType;
  cxLookupComboBox_ShipType.Properties.DropDownListStyle:=TcxEditDropDownListStyle.lsFixedList;
  cxLookupComboBox_ShipType.Properties.DropDownAutoSize:=True;                                        //设置下拉列表为自适应宽度
//    cxLookupComboBox_ShipType.Properties.ListFieldNames:=TSmShipTypeModel.cShipTypeNameKey;     //设置下拉列表需要显示的列
  cxLookupComboBox_ShipType.Properties.KeyFieldNames:=TSmShipTypeModel.cShipTypeIdKey;            //设置返回的字段名，用EditValue取值
  cxLookupComboBox_ShipType.Properties.ListFieldIndex:=0;

  Result:=True;
end;

function TFrame_smOrder.InitOrderDetail(out oError:string):Boolean;
var
  OrderDetailView: TcxGridDBBandedTableView;
begin
  OrderDetailView:=cxGrid_OrderDtl.Levels[0].GridView as TcxGridDBBandedTableView;
  OrderDetailView.ClearItems;
  case fRunMode of
  rmView:
    begin
      OrderDetailView.OptionsData.Editing:=False; //不允许编辑
    end;
  rmNew,rmEdit:
    begin
      OrderDetailView.OptionsData.Editing:=True; //允许编辑
    end;
  end;
  OrderDetailView.OptionsData.Appending:=False;        //不允许通过下键或回车新增行
  OrderDetailView.OptionsView.BandHeaders:=False;      //隐藏锁定列标题
  OrderDetailView.OptionsBehavior.FocusFirstCellOnNewRecord:=True; //新纪录焦点获取
  OrderDetailView.OptionsBehavior.GoToNextCellOnEnter:=True; //这一行的目的是用回车键自动换行
  OrderDetailView.OptionsBehavior.FocusCellOnCycle:=True; //对只允许行选择的回车键效果起作用
  OrderDetailView.OptionsView.GroupByBox:=False;  //取消表格默认分组头
  OrderDetailView.OptionsView.Footer:=True;       //开启页脚
  OrderDetailView.DataController.DataSource:=Ds_OrderDtl;

  with OrderDetailView.Bands.Add do
  begin
    FixedKind:=fkNone;
    Caption:='无锁定';
  end;
  with OrderDetailView.Bands.Add do
  begin
    FixedKind:=fkLeft;
    Caption:='左锁定';
  end;
//  with OrderDetailView.Bands.Add do
//  begin
//    FixedKind:=fkRight;
//    Caption:='右锁定';
//  end;

  if OrderDetailView.DataController.DataSource<>nil then
  begin
    with OrderDetailView.CreateColumn do
    begin
      DataBinding.FieldName := TSmOrderDtlModel.cItemNoKey;
      Caption := '物料编码';
      Width   :=100;
      PropertiesClass := TcxLookupComboBoxProperties;
      with Properties as TcxLookupComboBoxProperties do
      begin
        ImmediateDropDownWhenActivated:=True;
        ImmediateDropDownWhenKeyPressed:=True;
        ListSource:=Ds_ItemData;
        with ListColumns.Add  do
        begin
          Caption:='物料编码';
          FieldName:=TPmItemDataModel.cItemNoKey;
          MinWidth:=100;
          Width:=100;
        end;
        with ListColumns.Add  do
        begin
          Caption:='物料名称';
          FieldName:=TPmItemDataModel.cItemNameKey;
          MinWidth:=100;
          Width:=200;
        end;
        with ListColumns.Add  do
        begin
          Caption:='物料规格';
          FieldName:=TPmItemDataModel.cItemSpecKey;
          MinWidth:=100;
          Width:=200;
        end;
        DropDownListStyle:=TcxEditDropDownListStyle.lsEditFixedList;
        DropDownAutoSize:=True;             //设置下拉列表为自适应宽度              //设置下拉列表需要显示的列
        KeyFieldNames:=TPmItemDataModel.cItemNoKey;            //设置返回的字段名，用EditValue取值
        ListFieldIndex:=0;                  //设置返回输入框中的值，用EditText取值
        OnCloseUp:=OnOrderDtlLookComboboxCloseUp;
      end;
      Position.BandIndex:=1;
    end;

    with OrderDetailView.CreateColumn do
    begin
      DataBinding.FieldName := TSmOrderDtlModel.cItemNameKey;
      Caption := '物料名称';
      Width   :=150;
      PropertiesClass := TcxTextEditProperties;
      Options.Editing:=False;
      Position.BandIndex:=1;
    end;

    with OrderDetailView.CreateColumn do
    begin
      DataBinding.FieldName := TSmOrderDtlModel.cOrderDtlBackQtyKey;
      Caption := '归还数量';
      Width   :=120;
      PropertiesClass := TcxSpinEditProperties;
      Options.Editing:=True;
      with Properties as TcxSpinEditProperties do
      begin
        ValueType:=TcxSpinEditValueType.vtFloat;
        DisplayFormat:='0.00;-0.00';
        Increment:=1;
//        EditFormat:=                                                                                  //设置返回输入框中的值，用EditText取值
      end;
      Position.BandIndex:=1;
      Summary.FooterKind := skSum;
      Summary.FooterFormat:='0.00;-0.00';
    end;

    with OrderDetailView.CreateColumn do
    begin
      DataBinding.FieldName := TSmOrderDtlModel.cOrderDtlQtyKey;
      Caption := '销售数量';
      Width   :=120;
      PropertiesClass := TcxSpinEditProperties;
      Options.Editing:=True;
      with Properties as TcxSpinEditProperties do
      begin
        ValueType:=TcxSpinEditValueType.vtFloat;
        DisplayFormat:='0.00;-0.00';
        Increment:=1;
//        EditFormat:=                                                                                  //设置返回输入框中的值，用EditText取值
      end;
      Position.BandIndex:=1;
      Summary.FooterKind := skSum;
      Summary.FooterFormat:='0.00;-0.00';
    end;

    with OrderDetailView.CreateColumn do
    begin
      DataBinding.FieldName := TSmOrderDtlModel.cMsunitIdKey;
      Caption := '单位';
      Width:=50;
      Options.Editing:=False;
      PropertiesClass := TcxLookupComboBoxProperties;
      with Properties as TcxLookupComboBoxProperties do
      begin
        ImmediateDropDownWhenActivated:=False;
        ImmediateDropDownWhenKeyPressed:=False;
        ListSource:=Ds_Msunit;
        DropDownListStyle:=TcxEditDropDownListStyle.lsFixedList;
        DropDownAutoSize:=True;                                                                             //设置下拉列表为自适应宽度
        KeyFieldNames:=TPmMsunitModel.cMsunitIdKey;                                               //设置返回的字段名，用EditValue取值
        ListFieldNames:=TPmMsunitModel.cMsunitIdKey+';'+TPmMsunitModel.cMsunitNameKey;  //设置下拉列表需要显示的列
        ListFieldIndex:=1;                                                                                  //设置返回输入框中的值，用EditText取值
      end;
      Position.BandIndex:=0;
    end;

    with OrderDetailView.CreateColumn do
    begin
      DataBinding.FieldName := TSmOrderDtlModel.cOrderDtlPriceKey;
      Caption := '单价';
      Width   :=120;
      PropertiesClass := TcxCurrencyEditProperties;
      Options.Editing:=False;
      with Properties as TcxCurrencyEditProperties do
      begin
        DecimalPlaces:=2;
        DisplayFormat:='¥,0.00;¥-,0.00';
//        EditFormat:=                                                                                  //设置返回输入框中的值，用EditText取值
      end;
      Position.BandIndex:=0;
    end;

    with OrderDetailView.CreateColumn do
    begin
      DataBinding.FieldName := TSmOrderDtlModel.cOrderDtlAmountKey;
      Caption := '销售金额';
      Width   :=120;
      PropertiesClass := TcxCurrencyEditProperties;
      Options.Editing:=False;
      with Properties as TcxCurrencyEditProperties do
      begin
        DecimalPlaces:=2;
        DisplayFormat:='¥,0.00;¥-,0.00';
//        EditFormat:=                                                                                  //设置返回输入框中的值，用EditText取值
      end;
      Position.BandIndex:=0;
      Summary.FooterKind := skSum;
      Summary.FooterFormat:='¥,0.00;¥-,0.00';
    end;

    with OrderDetailView.CreateColumn do
    begin
      DataBinding.FieldName := TSmOrderDtlModel.cItemSpecKey;
      Caption := '规格型号';
      Width   :=100;
      PropertiesClass := TcxTextEditProperties;
      Options.Editing:=False;
      Position.BandIndex:=0;
    end;

    with OrderDetailView.CreateColumn do
    begin
      DataBinding.FieldName := TSmOrderDtlModel.cPackageTypeIdKey;
      Caption := '容器型号';
      Width:=150;
      Options.Editing:=False;
      PropertiesClass := TcxLookupComboBoxProperties;
      with Properties as TcxLookupComboBoxProperties do
      begin
        ImmediateDropDownWhenActivated:=False;
        ImmediateDropDownWhenKeyPressed:=False;
        ListSource:=Ds_PackageType;
        DropDownListStyle:=TcxEditDropDownListStyle.lsFixedList;
        DropDownAutoSize:=True;                                                                             //设置下拉列表为自适应宽度
        KeyFieldNames:=TPmPackageTypeModel.cPackageTypeIdKey;                                               //设置返回的字段名，用EditValue取值
        ListFieldNames:=TPmPackageTypeModel.cPackageTypeIdKey+';'+TPmPackageTypeModel.cPackageTypeNameKey;  //设置下拉列表需要显示的列
        ListFieldIndex:=1;                                                                                  //设置返回输入框中的值，用EditText取值
      end;
      Position.BandIndex:=0;
    end;

    with OrderDetailView.CreateColumn do
    begin
      DataBinding.FieldName := TSmOrderDtlModel.cRanksIdKey;
      Caption := '产品等级';
      Width:=150;
      Options.Editing:=False;
      PropertiesClass := TcxLookupComboBoxProperties;
      with Properties as TcxLookupComboBoxProperties do
      begin
        ImmediateDropDownWhenActivated:=False;
        ImmediateDropDownWhenKeyPressed:=False;
        ListSource:=Ds_Ranks;
        DropDownListStyle:=TcxEditDropDownListStyle.lsFixedList;
        DropDownAutoSize:=True;                                                                             //设置下拉列表为自适应宽度
        KeyFieldNames:=TPmRanksModel.cRanksIdKey;                                               //设置返回的字段名，用EditValue取值
        ListFieldNames:=TPmRanksModel.cRanksIdKey+';'+TPmRanksModel.cRanksNameKey;  //设置下拉列表需要显示的列
        ListFieldIndex:=1;                                                                                  //设置返回输入框中的值，用EditText取值
      end;
      Position.BandIndex:=0;
    end;

    with OrderDetailView.CreateColumn do
    begin
      DataBinding.FieldName := TSmOrderDtlModel.cOrderDtlRemarkKey;
      Caption := '备注';
      Width   :=200;
      PropertiesClass := TcxTextEditProperties;
      Options.Editing:=True;
      Position.BandIndex:=0;
    end;
  end;

  with TcxGridDBTableSummaryItem(OrderDetailView.DataController.Summary.FooterSummaryItems.Add) do
  begin
     Column:=OrderDetailView.GetColumnByFieldName(TSmOrderDtlModel.cOrderDtlPriceKey);
     FieldName:=TSmOrderDtlModel.cOrderDtlPriceKey;
     Kind:=skSum;
     Position:=spFooter;
     Format:='押金:¥,0.00;¥-,0.00';
  end;

  with TcxGridDBTableSummaryItem(OrderDetailView.DataController.Summary.FooterSummaryItems.Add) do
  begin
     Column:=OrderDetailView.GetColumnByFieldName(TSmOrderDtlModel.cItemNameKey);
     FieldName:=TSmOrderDtlModel.cItemNameKey;
     Kind:=skSum;
     Position:=spFooter;
     Format:='总金额:¥,0.00;¥-,0.00';
  end;
  OrderDetailView.DataController.Summary.FooterSummaryItems.OnSummary:=OnFooterSummary;
  Result:=True;
end;

function TFrame_smOrder.InitOrderCharge(out oError:string):Boolean;
begin
  cxGridTableView_ChargeItem.ClearItems;
  case fRunMode of
  rmView:
    begin
      cxGridTableView_ChargeItem.OptionsData.Editing:=False; //不允许编辑
    end;
  rmNew,rmEdit:
    begin
      cxGridTableView_ChargeItem.OptionsData.Editing:=True; //允许编辑
    end;
  end;
  cxGridTableView_ChargeItem.OptionsData.Appending:=False;        //不允许通过下键或回车新增行
  cxGridTableView_ChargeItem.OptionsView.BandHeaders:=False;      //隐藏锁定列标题
  cxGridTableView_ChargeItem.OptionsBehavior.FocusFirstCellOnNewRecord:=True; //新纪录焦点获取
  cxGridTableView_ChargeItem.OptionsBehavior.GoToNextCellOnEnter:=True; //这一行的目的是用回车键自动换行
  cxGridTableView_ChargeItem.OptionsBehavior.FocusCellOnCycle:=True; //对只允许行选择的回车键效果起作用
  cxGridTableView_ChargeItem.OptionsView.GroupByBox:=False;  //取消表格默认分组头
  cxGridTableView_ChargeItem.OptionsView.Footer:=True;       //开启页脚
  cxGridTableView_ChargeItem.DataController.DataSource:=Ds_OrderCharge;

  if cxGridTableView_ChargeItem.DataController.DataSource<>nil then
  begin
    with cxGridTableView_ChargeItem.CreateColumn do
    begin
      DataBinding.FieldName := TSmOrderChargeModel.cChargeItemIdKey;
      Caption := '收费项目';
      Width   :=100;
      Options.Editing:=True;
      PropertiesClass := TcxLookupComboBoxProperties;
      with Properties as TcxLookupComboBoxProperties do
      begin
        with ListColumns.Add  do
        begin
          Caption:='收费项编号';
          FieldName:=TSmChargeItemModel.cChargeItemIdKey;
          MinWidth:=100;
          Width:=100;
        end;
        with ListColumns.Add  do
        begin
          Caption:='收费项名称';
          FieldName:=TSmChargeItemModel.cChargeItemNameKey;
          MinWidth:=100;
          Width:=100;
        end;
        ImmediateDropDownWhenActivated:=False;
        ImmediateDropDownWhenKeyPressed:=False;
        ListSource:=Ds_ChargeItem;
        DropDownListStyle:=TcxEditDropDownListStyle.lsFixedList;
        DropDownAutoSize:=True;                                                                             //设置下拉列表为自适应宽度
        KeyFieldNames:=TSmChargeItemModel.cChargeItemIdKey;                                               //设置返回的字段名，用EditValue取值
//        ListFieldNames:=TSmChargeItemModel.cChargeItemIdKey+';'+TSmChargeItemModel.cChargeItemNameKey;  //设置下拉列表需要显示的列
        ListFieldIndex:=1;       //设置返回输入框中的值，用EditText取值
      end;
    end;

    with cxGridTableView_ChargeItem.CreateColumn do
    begin
      DataBinding.FieldName := TSmOrderChargeModel.cOrderChargeAmountKey;
      Caption := '金额';
      Width   :=100;
      PropertiesClass := TcxCurrencyEditProperties;
      Options.Editing:=True;
      with Properties as TcxCurrencyEditProperties do
      begin
        DecimalPlaces:=2;
        DisplayFormat:='¥,0.00;¥-,0.00';
//        EditFormat:=                                                                                  //设置返回输入框中的值，用EditText取值
      end;
      Summary.FooterKind := skSum;
      Summary.FooterFormat:='¥,0.00;¥-,0.00';
    end;

    with cxGridTableView_ChargeItem.CreateColumn do
    begin
      DataBinding.FieldName := TSmOrderChargeModel.cOrderChargeRemarkKey;
      Caption := '备注';
      Width   :=150;
      PropertiesClass := TcxTextEditProperties;
      Options.Editing:=True;
    end;
  end;
  Result:=True;
end;

procedure TFrame_smOrder.OnOrderDtlCdsValidate(Sender: TField);
begin
  if SameStr(Sender.FieldName,TSmOrderDtlModel.cItemNoKey) then
  begin
    if Cds_ItemData.Locate(TPmItemDataModel.cItemNoKey,Cds_OrderDtl.FieldByName(TSmOrderDtlModel.cItemNoKey).AsString,[]) then
    begin
      Cds_OrderDtl.FieldByName(TSmOrderDtlModel.cItemNameKey).AsString:=Cds_ItemData.FieldByName(TPmItemDataModel.cItemNameKey).AsString;
      Cds_OrderDtl.FieldByName(TSmOrderDtlModel.cItemSpecKey).AsString:=Cds_ItemData.FieldByName(TPmItemDataModel.cItemSpecKey).AsString;
      Cds_OrderDtl.FieldByName(TSmOrderDtlModel.cPackageTypeIdKey).AsString:=Cds_ItemData.FieldByName(TPmItemDataModel.cPackageTypeIdKey).AsString;
      Cds_OrderDtl.FieldByName(TSmOrderDtlModel.cRanksIdKey).AsString:=Cds_ItemData.FieldByName(TPmItemDataModel.cRanksIdKey).AsString;
      Cds_OrderDtl.FieldByName(TSmOrderDtlModel.cMsunitIdKey).AsString:=Cds_ItemData.FieldByName(TPmItemDataModel.cMsunitIdKey).AsString;
      if Cds_ItemOther.Locate(TPmItemOtherModel.cItemNoKey,Cds_OrderDtl.FieldByName(TSmOrderDtlModel.cItemNoKey).AsString,[]) then
      begin
        Cds_OrderDtl.FieldByName(TSmOrderDtlModel.cOrderDtlPriceKey).AsCurrency:=Cds_ItemOther.FieldByName(TPmItemOtherModel.cItemPriceKey).AsCurrency;
      end else begin
        Cds_OrderDtl.FieldByName(TSmOrderDtlModel.cOrderDtlPriceKey).AsCurrency:=0;
      end;
      Cds_OrderDtl.FieldByName(TSmOrderDtlModel.cOrderDtlQtyKey).AsFloat:=0;
    end else begin
      Cds_OrderDtl.FieldByName(TSmOrderDtlModel.cItemNameKey).AsString:=EmptyStr;
      Cds_OrderDtl.FieldByName(TSmOrderDtlModel.cItemSpecKey).AsString:=EmptyStr;
      Cds_OrderDtl.FieldByName(TSmOrderDtlModel.cPackageTypeIdKey).AsString:=EmptyStr;
      Cds_OrderDtl.FieldByName(TSmOrderDtlModel.cRanksIdKey).AsString:=EmptyStr;
      Cds_OrderDtl.FieldByName(TSmOrderDtlModel.cMsunitIdKey).AsString:=EmptyStr;
      Cds_OrderDtl.FieldByName(TSmOrderDtlModel.cOrderDtlPriceKey).AsCurrency:=0;
      Cds_OrderDtl.FieldByName(TSmOrderDtlModel.cOrderDtlQtyKey).AsFloat:=0;
    end;
  end else if SameStr(Sender.FieldName,TSmOrderDtlModel.cOrderDtlQtyKey) then
  begin
    Cds_OrderDtl.FieldByName(TSmOrderDtlModel.cOrderDtlAmountKey).AsFloat:=
          Cds_OrderDtl.FieldByName(TSmOrderDtlModel.cOrderDtlQtyKey).AsFloat*
          Cds_OrderDtl.FieldByName(TSmOrderDtlModel.cOrderDtlPriceKey).AsFloat;
  end;
end;

procedure TFrame_smOrder.OnOrderDtlLookComboboxCloseUp(Sender:TObject);
begin
  TcxGridDBTableView(cxGrid_OrderDtl.Levels[0].GridView).Controller.FocusedColumnIndex:=TcxGridDBTableView(cxGrid_OrderDtl.Levels[0].GridView).Controller.FocusedColumnIndex+1;
end;

procedure TFrame_smOrder.OnFooterSummary(ASender: TcxDataSummaryItems; Arguments: TcxSummaryEventArguments; var OutArguments: TcxSummaryEventOutArguments);
var
  OrderDetailView: TcxGridDBBandedTableView;
begin
  inherited;
  OrderDetailView:=cxGrid_OrderDtl.Levels[0].GridView as TcxGridDBBandedTableView;
  if SameStr(TcxDBDataSummaryItem(Arguments.SummaryItem).FieldName,
              TSmOrderDtlModel.cOrderDtlPriceKey) then
  begin
    OutArguments.Value:=OrderDetailView.DataController.Values[Arguments.RecordIndex,OrderDetailView.GetColumnByFieldName(TSmOrderDtlModel.cOrderDtlQtyKey).Index]*
                        OrderDetailView.DataController.Values[Arguments.RecordIndex,OrderDetailView.GetColumnByFieldName(TSmOrderDtlModel.cOrderDtlPriceKey).Index];
  end else if SameStr(TcxDBDataSummaryItem(Arguments.SummaryItem).FieldName,
                      TSmOrderDtlModel.cItemNameKey) then
  begin
    OutArguments.Value:=OrderDetailView.DataController.Values[Arguments.RecordIndex,OrderDetailView.GetColumnByFieldName(TSmOrderDtlModel.cOrderDtlQtyKey).Index]*
                        OrderDetailView.DataController.Values[Arguments.RecordIndex,OrderDetailView.GetColumnByFieldName(TSmOrderDtlModel.cOrderDtlPriceKey).Index]+
                        OrderDetailView.DataController.Values[Arguments.RecordIndex,OrderDetailView.GetColumnByFieldName(TSmOrderDtlModel.cOrderDtlAmountKey).Index];
//    取页脚值
//    with OrderDetailView.DataController.Summary do
//    begin
//      OpsYaJin:=FooterSummaryItems.IndexOfItemLink(OrderDetailView.GetColumnByFieldName(TSmOrderDtlModel.cOrderDtlPriceKey));
//      if OpsYaJin<>-1 then
//      begin
//        OutArguments.Value:=FooterSummaryValues[OpsYaJin];
//      end;
//    end;
  end;
end;

procedure TFrame_smOrder.InitFrame(const iManageKey:string; const iRunMode:eRunMode; const iBillNo:string; const iParamStr:string);
var
  PiniYinStr,Key,Ids,Condition,Error:string;
  ParamJson:TQJson;
begin
  inherited InitFrame(iManageKey,iRunMode,iBillNo,iParamStr);
  ParamJson:=TQJson.Create;
  try
    if ParamJson.TryParse(iParamStr) then
    begin
      if ParamJson.ItemByName('OrderListManageKey')<>nil then
      begin
        fOrderListManageKey:=ParamJson.ItemByName('OrderListManageKey').AsString;
      end else begin
        fOrderListManageKey:=EmptyStr;
      end;
    end else begin
      fOrderListManageKey:=EmptyStr;
    end;
  finally
    ParamJson.Free;
  end;

  case fRunMode of
  rmView:
    begin
      if fDeptService.GetCDSModel(Cds_Dept,Error) and
        fLoginOrgOwnUseDeptList.SaveToCDS(Cds_Dept,Error) then
      begin

      end else begin
        ShowMessage('装载用户所属部门失败!');
        Exit;
      end;

      if fCustomerFileService.LoadAll(LoginOrg.OrgId,fLoginOrgCustomerFileList,Error) and
        fCustomerFileService.GetCDSModel(Cds_Customer,Error,dblmView) and
        fLoginOrgCustomerFileList.SaveToCDS(Cds_Customer,Error) then
      begin
        Cds_Customer.First;
        while not Cds_Customer.Eof do
        begin
          //获取每条记录的拼音缩写
          PiniYinStr:=GetStrPYIndex(Cds_Customer.FieldByName(TCmCustomerFileModel.cCustomerFullNameKey).AsString
                          +'|'+Cds_Customer.FieldByName(TCmCustomerFileModel.cCustomerSimpleNameKey).AsString);
          Cds_Customer.Edit;
          Cds_Customer.FieldByName('PinYin').AsString:=PiniYinStr;
          Cds_Customer.Post;
          Cds_Customer.Next;
        end;
        Ds_Customer.DataSet:=Cds_Customer;
      end else begin
        Exit;
        ShowMessage('装载客户基础数据失败!');
      end;

      if fItemOtherService.LoadAll(LoginOrg.OrgId,fLoginOrgItemOtherList,Error) and
        fItemOtherService.GetCDSModel(Cds_ItemOther,Error) and
        fLoginOrgItemOtherList.SaveToCDS(Cds_ItemOther,Error) then
      begin
        Ds_ItemOther.DataSet:=Cds_ItemOther;
        Ids:=EmptyStr;
        for Key in fLoginOrgItemOtherList.SortKeys do
        begin
          Ids:=''''+fLoginOrgItemOtherList.Items[Key].ItemNo+''',';
        end;
        if not SameStr(Trim(Ids),EmptyStr) then
        begin
          Ids:=IfThen(Length(Ids)>0,LeftStr(Ids,Length(Ids)-1),EmptyStr);
          Condition:=Format(string('%s IN (%s)'),[TPmItemDataModel.cItemNoKey,Ids]);
          if fItemDataService.LoadByCondition(fLoginOrgItemDataList,Condition,Error) and
            fItemDataService.GetCDSModel(Cds_ItemData,Error) and
            fLoginOrgItemDataList.SaveToCDS(Cds_ItemData,Error) then
          begin
            Ds_ItemData.DataSet:=Cds_ItemData;
          end else begin
            ShowMessage('装载物料主文件失败!');
            Exit;
          end;
        end;
      end else begin
        ShowMessage('装载本组织物料失败!');
        Exit;
      end;

      if fMsunitService.LoadAll(fLoginOrgMsunitList,Error) and
        fMsunitService.GetCDSModel(Cds_Msunit,Error) and
        fLoginOrgMsunitList.SaveToCDS(Cds_Msunit,Error) then
      begin
        Ds_Msunit.DataSet:=Cds_Msunit;
      end else begin
        ShowMessage('装载单位数据失败!');
        Exit;
      end;

      if fPackageTypeService.LoadAll(fLoginOrgPackageTypeList,Error) and
        fPackageTypeService.GetCDSModel(Cds_PackageType,Error) and
        fLoginOrgPackageTypeList.SaveToCDS(Cds_PackageType,Error) then
      begin
        Ds_PackageType.DataSet:=Cds_PackageType;
      end else begin
        ShowMessage('装载封装类型数据失败!');
        Exit;
      end;

      if fRanksService.LoadAll(fLoginOrgRanksList,Error) and
        fRanksService.GetCDSModel(Cds_Ranks,Error) and
        fLoginOrgRanksList.SaveToCDS(Cds_Ranks,Error) then
      begin
        Ds_Ranks.DataSet:=Cds_Ranks;
      end else begin
        ShowMessage('装载物料等级数据失败!');
        Exit;
      end;

      if fChargeItemService.LoadAll(fLoginOrgChargeItemList,Error) and
        fChargeItemService.GetCDSModel(Cds_ChargeItem,Error) and
        fLoginOrgChargeItemList.SaveToCDS(Cds_ChargeItem,Error) then
      begin
        Ds_ChargeItem.DataSet:=Cds_ChargeItem;
      end else begin
        ShowMessage('装载收费项目数据失败!');
        Exit;
      end;

      if fInvoiceTypeService.LoadAll(fLoginOrgInvoiceTypeList,Error) and
        fInvoiceTypeService.GetCDSModel(Cds_InvoiceType,Error) and
        fLoginOrgInvoiceTypeList.SaveToCDS(Cds_InvoiceType,Error) then
      begin
        Ds_InvoiceType.DataSet:=Cds_InvoiceType;
      end else begin
        ShowMessage('装载收费项目数据失败!');
        Exit;
      end;

      if fPaywayService.LoadAll(fLoginOrgPaywayList,Error) and
        fPaywayService.GetCDSModel(Cds_Payway,Error) and
        fLoginOrgPaywayList.SaveToCDS(Cds_Payway,Error) then
      begin
        Ds_Payway.DataSet:=Cds_Payway;
      end else begin
        ShowMessage('装载支付方式数据失败!');
        Exit;
      end;

      if fShipTypeService.LoadAll(fLoginOrgShipTypeList,Error) and
        fShipTypeService.GetCDSModel(Cds_ShipType,Error) and
        fLoginOrgShipTypeList.SaveToCDS(Cds_ShipType,Error) then
      begin
        Ds_ShipType.DataSet:=Cds_ShipType;
      end else begin
        ShowMessage('装载运输方式数据失败!');
        Exit;
      end;
    end;
  rmNew,rmEdit:
    begin
      if fDeptService.GetCDSModel(Cds_Dept,Error) then
      begin
        for Key in fLoginUserInOrgBelongUseDeptList.SortKeys do
        begin
          if fLoginOrgOwnAllDeptList.Items[fLoginUserInOrgBelongUseDeptList.Items[Key].DeptId]<>nil then
          begin
            if not fLoginOrgOwnAllDeptList.Items[fLoginUserInOrgBelongUseDeptList.Items[Key].DeptId].SaveToCDS(Cds_Dept,Error) then
            begin
              ShowMessage('装载用户所属部门失败!');
              Exit;
            end;
          end;
        end;
      end else begin
        ShowMessage('装载用户所属部门失败!');
        Exit;
      end;

      if fCustomerFileService.LoadAll(LoginOrg.OrgId,fLoginOrgCustomerFileList,Error,True) and
        fCustomerFileService.GetCDSModel(Cds_Customer,Error,dblmView) and
        fLoginOrgCustomerFileList.SaveToCDS(Cds_Customer,Error) then
      begin
        Cds_Customer.First;
        while not Cds_Customer.Eof do
        begin
          //获取每条记录的拼音缩写
          PiniYinStr:=GetStrPYIndex(Cds_Customer.FieldByName(TCmCustomerFileModel.cCustomerFullNameKey).AsString
                          +'|'+Cds_Customer.FieldByName(TCmCustomerFileModel.cCustomerSimpleNameKey).AsString);
          Cds_Customer.Edit;
          Cds_Customer.FieldByName('PinYin').AsString:=PiniYinStr;
          Cds_Customer.Post;
          Cds_Customer.Next;
        end;
        Ds_Customer.DataSet:=Cds_Customer;
      end else begin
        Exit;
        ShowMessage('装载客户基础数据失败!');
      end;

      if fItemOtherService.LoadAll(LoginOrg.OrgId,fLoginOrgItemOtherList,Error,True) and
        fItemOtherService.GetCDSModel(Cds_ItemOther,Error) and
        fLoginOrgItemOtherList.SaveToCDS(Cds_ItemOther,Error) then
      begin
        Ds_ItemOther.DataSet:=Cds_ItemOther;
        Ids:=EmptyStr;
        for Key in fLoginOrgItemOtherList.SortKeys do
        begin
          Ids:=''''+fLoginOrgItemOtherList.Items[Key].ItemNo+''',';
        end;
        if not SameStr(Trim(Ids),EmptyStr) then
        begin
          Ids:=IfThen(Length(Ids)>0,LeftStr(Ids,Length(Ids)-1),EmptyStr);
          Condition:=Format(string('%s IN (%s)'),[TPmItemDataModel.cItemNoKey,Ids]);
          if fItemDataService.LoadByCondition(fLoginOrgItemDataList,Condition,Error) and
            fItemDataService.GetCDSModel(Cds_ItemData,Error) and
            fLoginOrgItemDataList.SaveToCDS(Cds_ItemData,Error) then
          begin
            Ds_ItemData.DataSet:=Cds_ItemData;
          end else begin
            ShowMessage('装载物料主文件失败!');
            Exit;
          end;
        end;
      end else begin
        ShowMessage('装载本组织物料失败!');
        Exit;
      end;

      if fMsunitService.LoadAll(fLoginOrgMsunitList,Error,True) and
        fMsunitService.GetCDSModel(Cds_Msunit,Error) and
        fLoginOrgMsunitList.SaveToCDS(Cds_Msunit,Error) then
      begin
        Ds_Msunit.DataSet:=Cds_Msunit;
      end else begin
        ShowMessage('装载单位数据失败!');
        Exit;
      end;

      if fPackageTypeService.LoadAll(fLoginOrgPackageTypeList,Error,True) and
        fPackageTypeService.GetCDSModel(Cds_PackageType,Error) and
        fLoginOrgPackageTypeList.SaveToCDS(Cds_PackageType,Error) then
      begin
        Ds_PackageType.DataSet:=Cds_PackageType;
      end else begin
        ShowMessage('装载封装类型数据失败!');
        Exit;
      end;

      if fRanksService.LoadAll(fLoginOrgRanksList,Error,True) and
        fRanksService.GetCDSModel(Cds_Ranks,Error) and
        fLoginOrgRanksList.SaveToCDS(Cds_Ranks,Error) then
      begin
        Ds_Ranks.DataSet:=Cds_Ranks;
      end else begin
        ShowMessage('装载物料等级数据失败!');
        Exit;
      end;

      if fChargeItemService.LoadAll(fLoginOrgChargeItemList,Error,True) and
        fChargeItemService.GetCDSModel(Cds_ChargeItem,Error) and
        fLoginOrgChargeItemList.SaveToCDS(Cds_ChargeItem,Error) then
      begin
        Ds_ChargeItem.DataSet:=Cds_ChargeItem;
      end else begin
        ShowMessage('装载收费项目数据失败!');
        Exit;
      end;

      if fInvoiceTypeService.LoadAll(fLoginOrgInvoiceTypeList,Error,True) and
        fInvoiceTypeService.GetCDSModel(Cds_InvoiceType,Error) and
        fLoginOrgInvoiceTypeList.SaveToCDS(Cds_InvoiceType,Error) then
      begin
        Ds_InvoiceType.DataSet:=Cds_InvoiceType;
      end else begin
        ShowMessage('装载收费项目数据失败!');
        Exit;
      end;

      if fPaywayService.LoadAll(fLoginOrgPaywayList,Error,True) and
        fPaywayService.GetCDSModel(Cds_Payway,Error) and
        fLoginOrgPaywayList.SaveToCDS(Cds_Payway,Error) then
      begin
        Ds_Payway.DataSet:=Cds_Payway;
      end else begin
        ShowMessage('装载支付方式数据失败!');
        Exit;
      end;

      if fShipTypeService.LoadAll(fLoginOrgShipTypeList,Error,True) and
        fShipTypeService.GetCDSModel(Cds_ShipType,Error) and
        fLoginOrgShipTypeList.SaveToCDS(Cds_ShipType,Error) then
      begin
        Ds_ShipType.DataSet:=Cds_ShipType;
      end else begin
        ShowMessage('装载运输方式数据失败!');
        Exit;
      end;
    end;
  end;

  if fOrderDtlService.GetCDSModel(Cds_OrderDtl,Error) and
    fOrderChargeService.GetCDSModel(Cds_OrderCharge,Error)  then
  begin
    Ds_OrderDtl.DataSet:=Cds_OrderDtl;
    Cds_OrderDtl.FieldByName(TSmOrderDtlModel.cItemNoKey).OnValidate:=OnOrderDtlCdsValidate;
    Cds_OrderDtl.FieldByName(TSmOrderDtlModel.cOrderDtlQtyKey).OnValidate:=OnOrderDtlCdsValidate;

    Ds_OrderCharge.DataSet:=Cds_OrderCharge;
  end else begin
    ShowMessage('装载订单明细、收费项模型数据失败!');
    Exit;
  end;

  if InitOrderMast(Error) and
    InitOrderDetail(Error) and
    InitOrderCharge(Error) then
  begin
    case fRunMode of
    rmView,rmEdit:
      begin
        Condition:=Format(string('%s=''%s'''),[TSmOrderMstModel.cOrderIdKey,fBillNo]);
        if not fOrderService.LoadByCondition(fCurrOrder,Condition,Error) then
        begin
          ShowMessage('装载订单数据失败!');
          Exit;
        end;
      end;
    rmNew:
      begin

      end;
    end;
    RefreshFrame(fCurrOrder,fRunMode);
  end else begin
    ShowMessage(Format(string('初始化界面失败! 错误内容:%s'),[Error]));
    Exit;
  end;
end;

procedure TFrame_smOrder.RefreshComponent(const iItem:TSmOrder; const iRunMode:eRunMode);
begin
  LabelEdit_OrderId.ReadOnly:=True;
  case iRunMode of
  rmView:
    begin
      cxLookupComboBox_Dept.Properties.ReadOnly:=True;
      cxLookupComboBox_Customer.Properties.ReadOnly:=True;
      cxLookupComboBox_EmpId.Properties.ReadOnly:=True;
      cxLookupComboBox_Linkman.Properties.ReadOnly:=True;
      cxLookupComboBox_InvoiceType.Properties.ReadOnly:=True;
      cxLookupComboBox_Payway.Properties.ReadOnly:=True;
      cxLookupComboBox_ShipType.Properties.ReadOnly:=True;
      LabelEdit_Remark.ReadOnly:=True;
    end;
  rmNew:
    begin
      cxLookupComboBox_Dept.Properties.ReadOnly:=False;
      cxLookupComboBox_Customer.Properties.ReadOnly:=False;
      cxLookupComboBox_EmpId.Properties.ReadOnly:=False;
      cxLookupComboBox_Linkman.Properties.ReadOnly:=False;
      cxLookupComboBox_InvoiceType.Properties.ReadOnly:=False;
      cxLookupComboBox_Payway.Properties.ReadOnly:=False;
      cxLookupComboBox_ShipType.Properties.ReadOnly:=False;
      LabelEdit_Remark.ReadOnly:=False;
    end;
  rmEdit:
    begin
      if iItem<>nil then
      begin
        cxLookupComboBox_Dept.Properties.ReadOnly:=False;
        cxLookupComboBox_Customer.Properties.ReadOnly:=False;
        cxLookupComboBox_EmpId.Properties.ReadOnly:=False;
        cxLookupComboBox_Linkman.Properties.ReadOnly:=False;
        cxLookupComboBox_InvoiceType.Properties.ReadOnly:=False;
        cxLookupComboBox_Payway.Properties.ReadOnly:=False;
        cxLookupComboBox_ShipType.Properties.ReadOnly:=False;
        LabelEdit_Remark.ReadOnly:=False;
      end else begin
        cxLookupComboBox_Dept.Properties.ReadOnly:=True;
        cxLookupComboBox_Customer.Properties.ReadOnly:=True;
        cxLookupComboBox_EmpId.Properties.ReadOnly:=True;
        cxLookupComboBox_Linkman.Properties.ReadOnly:=True;
        cxLookupComboBox_InvoiceType.Properties.ReadOnly:=True;
        cxLookupComboBox_Payway.Properties.ReadOnly:=True;
        cxLookupComboBox_ShipType.Properties.ReadOnly:=True;
        LabelEdit_Remark.ReadOnly:=True;
      end;
    end;
  end;
end;

procedure TFrame_smOrder.RefreshButton(const iItem:TSmOrder; const iRunMode:eRunMode);
begin

end;

procedure TFrame_smOrder.RefreshFrame(const iItem:TSmOrder; const iRunMode:eRunMode);
begin
  RefreshComponent(iItem,iRunMode);
  RefreshButton(iItem,iRunMode);
  Obj2Frame(iItem,iRunMode);
end;

procedure TFrame_smOrder.Obj2Frame(const iItem:TSmOrder; const iRunMode:eRunMode);
var
  Error:string;
begin
  case iRunMode of
  rmView,rmEdit:
    begin
      if iItem<>nil then
      begin
        LabelEdit_OrderId.Text:=iItem.OrderId;
        cxLookupComboBox_Dept.EditValue:=iItem.DeptId;
        cxLookupComboBox_Customer.EditValue:=iItem.CustomerId;
        cxLookupComboBox_EmpId.EditValue:=iItem.EmpId;
        cxLookupComboBox_Linkman.EditValue:=iItem.LinkmanId;
        cxLookupComboBox_InvoiceType.EditValue:=iItem.InvoiceTypeId;
        cxLookupComboBox_Payway.EditValue:=iItem.PaywayId;
        cxLookupComboBox_ShipType.EditValue:=iItem.ShipTypeId;
        LabelEdit_Remark.Text:=iItem.OrderRemark;
        Cds_OrderDtl.EmptyDataSet;
        if not iItem.OrderDetailList.SaveToCDS(Cds_OrderDtl,Error) then
        begin
          ShowMessage('订单明细对象到数据集失败...');
        end;

        Cds_OrderCharge.EmptyDataSet;
        if not iItem.OrderChargeList.SaveToCDS(Cds_OrderCharge,Error) then
        begin
          ShowMessage('收费项对象到数据集失败...');
        end;
      end else begin
        LabelEdit_OrderId.Text:=EmptyStr;
        cxLookupComboBox_Dept.EditValue:=EmptyStr;
        cxLookupComboBox_Customer.EditValue:=EmptyStr;
        cxLookupComboBox_EmpId.EditValue:=EmptyStr;
        cxLookupComboBox_Linkman.EditValue:=EmptyStr;
        cxLookupComboBox_InvoiceType.EditValue:=EmptyStr;
        cxLookupComboBox_Payway.EditValue:=EmptyStr;
        cxLookupComboBox_ShipType.EditValue:=EmptyStr;
        LabelEdit_Remark.Text:=EmptyStr;
        Cds_OrderDtl.EmptyDataSet;
      end;
    end;
  rmNew:
    begin
      LabelEdit_OrderId.Text:=EmptyStr;
      cxLookupComboBox_Dept.EditValue:=EmptyStr;
      cxLookupComboBox_Customer.EditValue:=EmptyStr;
      cxLookupComboBox_EmpId.EditValue:=EmptyStr;
      cxLookupComboBox_Linkman.EditValue:=EmptyStr;
      cxLookupComboBox_InvoiceType.EditValue:=EmptyStr;
      cxLookupComboBox_Payway.EditValue:=EmptyStr;
      cxLookupComboBox_ShipType.EditValue:=EmptyStr;
      LabelEdit_Remark.Text:=EmptyStr;
      Cds_OrderDtl.EmptyDataSet;
    end;
  end;
end;

procedure TFrame_smOrder.Frame2Obj(const iItem:TSmOrder; const iRunMode:eRunMode);
var
  OrderDetailView: TcxGridDBBandedTableView;
  Error:string;
begin
  OrderDetailView:=cxGrid_OrderDtl.Levels[0].GridView as TcxGridDBBandedTableView;
  case iRunMode of
  rmNew:
    begin
      if iItem<>nil then
      begin
        iItem.OrderId:=LabelEdit_OrderId.Text;
        iItem.DeptId:=cxLookupComboBox_Dept.EditValue;
        iItem.CustomerId:=cxLookupComboBox_Customer.EditValue;
        iItem.EmpId:=cxLookupComboBox_EmpId.EditValue;
        iItem.LinkmanId:=cxLookupComboBox_Linkman.EditValue;
        iItem.InvoiceTypeId:=cxLookupComboBox_InvoiceType.EditValue;
        iItem.PaywayId:=cxLookupComboBox_Payway.EditValue;
        iItem.ShipTypeId:=cxLookupComboBox_ShipType.EditValue;
        iItem.OrderRemark:=LabelEdit_Remark.Text;

        iItem.OrderBillType:=1;
        iItem.OrderStatus:=0;
        iItem.OrgId:=fLoginOrg.OrgId;

        iItem.OrderSendStatus:=2;
        iItem.OrderSettlementStatus:=0;
        iItem.RecordPsn:=fLoginUser.UserId;
        iItem.RecordDate:=Now;

        if (Cds_OrderDtl.State=dsEdit) or (Cds_OrderDtl.State=dsInsert) then
        begin
          Cds_OrderDtl.Post;
        end;
        if not iItem.OrderDetailList.LoadFromCDS(Cds_OrderDtl,Error) then
        begin
          ShowMessage('装载明细项数据失败!');
          Exit;
        end;

        if (Cds_OrderCharge.State=dsEdit) or (Cds_OrderCharge.State=dsInsert) then
        begin
          Cds_OrderCharge.Post;
        end;
        if not iItem.OrderChargeList.LoadFromCDS(Cds_OrderCharge,Error) then
        begin
          ShowMessage('装载收费项数据失败!');
          Exit;
        end;
      end;
    end;
  rmEdit:
    begin
      if iItem<>nil then
      begin
        iItem.OrderId:=LabelEdit_OrderId.Text;
        iItem.DeptId:=cxLookupComboBox_Dept.EditValue;
        iItem.CustomerId:=cxLookupComboBox_Customer.EditValue;
        iItem.EmpId:=cxLookupComboBox_EmpId.EditValue;
        iItem.LinkmanId:=cxLookupComboBox_Linkman.EditValue;
        iItem.InvoiceTypeId:=cxLookupComboBox_InvoiceType.EditValue;
        iItem.PaywayId:=cxLookupComboBox_Payway.EditValue;
        iItem.ShipTypeId:=cxLookupComboBox_ShipType.EditValue;
        iItem.OrderRemark:=LabelEdit_Remark.Text;

        iItem.OrderBillType:=1;
        iItem.OrderStatus:=0;
        iItem.OrgId:=fLoginOrg.OrgId;

        iItem.OrderSendStatus:=2;
        iItem.OrderSettlementStatus:=0;
        iItem.ModifyPsn:=fLoginUser.UserId;
        iItem.ModifyDate:=Now;

        if (Cds_OrderDtl.State=dsEdit) or (Cds_OrderDtl.State=dsInsert) then
        begin
          Cds_OrderDtl.Post;
        end;
        if not iItem.OrderDetailList.LoadFromCDS(Cds_OrderDtl,Error) then
        begin
          ShowMessage('装载明细项数据失败!');
          Exit;
        end;

        if (Cds_OrderCharge.State=dsEdit) or (Cds_OrderCharge.State=dsInsert) then
        begin
          Cds_OrderCharge.Post;
        end;
        if not iItem.OrderChargeList.LoadFromCDS(Cds_OrderCharge,Error) then
        begin
          ShowMessage('装载收费项数据失败!');
          Exit;
        end;
      end;
    end;
  rmView:
    begin
    end;
  end;
end;

end.
